package com.shrio.mail;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * Created by luojie on 2018/5/30.
 */
public class HTMLMailDemo {
    public static void main(String[] args) throws Exception{
        JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();

        // 设定mail server
        senderImpl.setHost("smtp.163.com");

        // 建立邮件消息,发送简单邮件和html邮件的区别
        MimeMessage mailMessage = senderImpl.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage);

        // 设置收件人，寄件人
        messageHelper.setTo("test@qq.com");
        messageHelper.setFrom("15708464616@163.com");
        messageHelper.setSubject("测试HTML邮件！");
        // true 表示启动HTML格式的邮件
        messageHelper
                .setText(
                        "<html><head></head><body><h1>hello!!spring html Mail</h1></body></html>",
                        true);

        senderImpl.setUsername("15708464616@163.com"); // 根据自己的情况,设置这里使用的是网易的邮箱
        senderImpl.setPassword("123456"); // 根据自己的情况, 设置password，这里的密码是网易邮箱的smtp.163.com 的密码
        Properties prop = new Properties();
        prop.put("mail.smtp.auth", "true"); // 将这个参数设为true，让服务器进行认证,认证用户名和密码是否正确
        prop.put("mail.smtp.timeout", "25000");
        senderImpl.setJavaMailProperties(prop);
        // 发送邮件
        senderImpl.send(mailMessage);

        System.out.println("邮件发送成功..");
    }
}
